<!--
 * @Description: 缺陷等级新增或编辑
 * @Author: Liu Zhiwang
 * @Date: 2021-04-13 17:05:40
 * @LastEditTime: 2021-04-22 15:38:06
 * @LastEditors: Liu Zhiwang
-->
<template>
  <div>
    <base-dialog
      class="dialog_box"
      :visible.sync="dialog.visible"
      :title="dialog.title"
      :loading="dialog.loading"
      :disabled="dialog.disabled"
      @updateData="updateData"
      @open="beforeOpenDialog">
      <el-form
        ref="dataForm"
        :model="dialog.form"
        :rules="getRules"
        :disabled="dialog.disabled"
        label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="等级名称" prop="levelName">
              <el-input v-model="dialog.form.levelName" clearable :maxlength="20" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="等级编号" prop="levelCode">
              <el-input v-model="dialog.form.levelCode" clearable :maxlength="20" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="整改期限" prop="solveTimeCount">
              <el-input v-model="dialog.form.solveTimeCount" clearable :maxlength="20">
                <template slot="append">天</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提醒期限" prop="remindTimeCount">
              <el-input v-model="dialog.form.remindTimeCount" clearable :maxlength="20">
                <template slot="append">天</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="等级描述" prop="description">
              <el-input v-model="dialog.form.description" type="textarea" :autosize="{ minRows: 4}" maxlength="500" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </base-dialog>
  </div>
</template>
<script>
export default {
  props: {
    dialog: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      lineNameTree: []
    }
  },
  computed: {
    getRules() {
      var rules = this.dialog.rules
      rules.solveTimeCount.push({ validator: this.validateSTC, trigger: 'blur' })
      rules.remindTimeCount.push({ validator: this.validateRTC, trigger: 'blur' })
      return rules
    }
  },
  watch: {},
  methods: {
    beforeOpenDialog() {
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    validateSTC(rule, value, callback) {
      // if (Number(this.dialog.form.remindTimeCount) >= Number(this.dialog.form.solveTimeCount)) {
      //   callback(new Error('提醒期限要大于整改期限'))
      // }
      if (this.dialog.form.remindTimeCount) {
        this.$refs.dataForm.validateField('remindTimeCount')
      }
      callback()
    },
    validateRTC(rule, value, callback) {
      if (Number(this.dialog.form.remindTimeCount) >= Number(this.dialog.form.solveTimeCount)) {
        callback(new Error('提醒期限要小于整改期限'))
      }
      callback()
    },
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.dialog.loading = true
          this.$emit('updateData', this.dialog.form)
        }
      })
    },
    changeLine() {},
    clearLine() {}
  }
}
</script>
